<!doctype html><html lang=cn-zh><head><meta charset=UTF-8><meta name=viewport content="width=device-width,initial-scale=1"><meta http-equiv=X-UA-Compatible content="IE=edge"><style type=text/css>body{font-family:monospace}</style><title>Python-from __future__ import print_function</title>
<meta name=description content="A blog maintained by Vimiix."><link rel=stylesheet href=/css/style.css><script>var _hmt=_hmt||[];(function(){var e,t=document.createElement("script");t.src="https://hm.baidu.com/hm.js?7c24231917964240bae97e813810620c",e=document.getElementsByTagName("script")[0],e.parentNode.insertBefore(t,e)})()</script></head><body><header>====================<br>== Hi, I'm Vimiix ==<br>====================<div style=float:right;color:gray;font-size:x-large>Get hands dirty.</div><br><p><nav><a href=https://www.vimiix.com/><b>首页</b></a>.
<a href=/posts/><b>文章列表</b></a>.
<a href=/projects/><b>开源项目</b></a>.
<a href=/tags/><b>标签</b></a>.
<a href=/friends/><b>友链</b></a>.
<a href=/about/><b>关于我</b></a>.
<a href=/index.xml><b>RSS</b></a>.</nav></p></header><main><article><h1>Python-from __future__ import print_function</h1><b><time>2017.05.12 14:55</time></b>
<a href=/tags/future-module>future module</a>
<a href=/tags/python>Python</a><div><p>Python 提供了**__future__**模块，把下一个新版本的特性导入到当前版本，于是我们就可以在当前版本中测试一些新版本的特性</p><p>在 python2.x 的环境是使用下面语句，则第二句语法检查通过，第三句语法检查失败</p><div class=highlight><pre tabindex=0 style=color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4><code class=language-Python data-lang=Python><span style=display:flex><span>	<span style=color:#f92672>from</span> __future__ <span style=color:#f92672>import</span> print_function
</span></span><span style=display:flex><span>	print(<span style=color:#e6db74>&#39;good&#39;</span>)  <span style=color:#75715e>#可以通过执行</span>
</span></span><span style=display:flex><span>	print <span style=color:#e6db74>&#39;bad&#39;</span>    <span style=color:#75715e>#语法错误</span>
</span></span></code></pre></div><p>在 python2.x 中，默认的<strong>print</strong>只是一个简单的输出流方法，不带有任何的参数。</p><p>用下面的两个例子概况：</p><p><strong>示例 1</strong></p><div class=highlight><pre tabindex=0 style=color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4><code class=language-Python data-lang=Python><span style=display:flex><span>	var, var1, var2 <span style=color:#f92672>=</span> <span style=color:#ae81ff>1</span>, <span style=color:#ae81ff>2</span>, <span style=color:#ae81ff>3</span>
</span></span><span style=display:flex><span>	print var
</span></span><span style=display:flex><span>	print var1, var2
</span></span></code></pre></div><p>示例 1 会打印两行，在第二行中两个数字之间会有一个空格。</p><p><strong>示例 2</strong></p><div class=highlight><pre tabindex=0 style=color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4><code class=language-Python data-lang=Python><span style=display:flex><span>	<span style=color:#66d9ef>for</span> i <span style=color:#f92672>in</span> xrange(<span style=color:#ae81ff>10</span>):
</span></span><span style=display:flex><span>		print i,
</span></span></code></pre></div><p>示例 2 会将每个数字打印在一行上，每个数字之间有一个空格分割。如果去掉打印时 i 后面的“,”,每个数字会单独占一行。</p><p>下面从**__future__<strong>众引入对于 python2 来说比较先进的模块</strong>print_function**</p><p>接口参数：</p><div class=highlight><pre tabindex=0 style=color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4><code class=language-Python data-lang=Python><span style=display:flex><span>	print(<span style=color:#f92672>*</span>values, sep<span style=color:#f92672>=</span><span style=color:#e6db74>&#39; &#39;</span>, end<span style=color:#f92672>=</span><span style=color:#e6db74>&#39;</span><span style=color:#ae81ff>\n</span><span style=color:#e6db74>&#39;</span>, file<span style=color:#f92672>=</span>sys<span style=color:#f92672>.</span>stdout)
</span></span><span style=display:flex><span>	print(value1, value2, value3, sep<span style=color:#f92672>=</span><span style=color:#e6db74>&#39; &#39;</span>, end<span style=color:#f92672>=</span><span style=color:#e6db74>&#39;</span><span style=color:#ae81ff>\n</span><span style=color:#e6db74>&#39;</span>, file<span style=color:#f92672>=</span>sys<span style=color:#f92672>.</span>stdout)
</span></span></code></pre></div><p>这里，输出的变量可以是一个序列或者多个变量，可以像上面一样用逗号分开每个变量。 参数<code>sep</code>,<code>end</code>,<code>file</code>是三个可选参数。</p><p><code>sep</code> 指每个输出变量之间的分隔符，默认是一个空格</p><p><code>end</code> 指的是输出结束后的内容，默认是换行</p><p><code>file</code>指的是输出流要输出的目的文件，默认 sys.stdout（标准输出）</p><p>在 Pyhton2 中，<code>print_function</code> 比默认的 <code>print</code>效率要快很多！</p></div></article></main><aside><div><div><h3>LATEST POSTS</h3></div><div><ul><li><a href=/posts/2025-10-16-kubernetes-apiserver-authorization-mechanism/>Kubernetes APIServer 鉴权机制</a></li><li><a href=/posts/2025-09-30-kubernetes-apiserver-authentication-mechanism/>Kubernetes APIServer 认证机制</a></li><li><a href=/posts/2024-12-16-deploy-kubernetes-by-kubeadm/>使用 kubeadm 搭建 kubernetes 集群</a></li><li><a href=/posts/2024-09-20-how-to-code-review/>如何做code review</a></li><li><a href=/posts/2024-08-12-weakref-in-python/>Python中的弱引用</a></li></ul></div></div></aside><footer><p>Social Links:
<a href=https://github.com/vimiix><b>Github</b></a>.
<a href=https://www.douban.com/people/vimiix/><b>Douban</b></a>.
<a href=mailto:i@vimiix.com><b>Email</b></a>.<br><hr>&copy; 2017-2025
Vimiix Yao; All rights reserved.
<a href=https://beian.miit.gov.cn/>京ICP备19015214号-1</a></p><script src=https://l2dwidget.js.org/lib/L2Dwidget.min.js></script><script>L2Dwidget.init({model:{jsonPath:"https://unpkg.com/live2d-widget-model-tororo@1.0.5/assets/tororo.model.json"}})</script></footer></body></html>